﻿namespace VfNewMessages
{
    using System;
    using System.Windows.Forms;
    using JanusLibAing;
    using Rsdn.Janus;
    using Rsdn.Janus.CoreServices.Extensibility;
    using Rsdn.Janus.CoreServices.NavigationTree;
    using Rsdn.Janus.CoreServices.NavigationTree.Extensibility;
    using Rsdn.SmartApp;

    [VirtualForumProvider]
    public class NewMessagesProvider : IVirtualForumProvider
    {
        private readonly IServiceProvider provider;
        private ISharedInfoService sharedInfoService;

        //List<NavigationTreeNode> node = new List<NavigationTreeNode>();
        private VfNewMessagesItemInfo forumInfo;

        public NewMessagesProvider(IServiceProvider provider)
        {
            this.provider = provider;
            if (provider == null)
            {
                throw new ArgumentNullException("provider");          //MLHIDE
            }

            forumInfo = new VfNewMessagesItemInfo(provider);
            sharedInfoService = provider.GetRequiredService<ISharedInfoService>();

            //INavigationItemHeader header = new StaticNavigationItemHeader("HeaderTest");
            //node.Add(new NavigationTreeNode("Test1", header, "PageNameTest", null, 100));
        }

        #region Implementation of INavigationTreeProvider

        //public IEnumerable<INavigationTreeNodeSource> CreateNodes(IServiceProvider serviceProvider)
        //{
        //    return node.ToArray();
        //}

        public Func<object, bool> IsUpdateNeedFunc
        {
            get { return IsUpdateNeed; }
        }

        public Action<int, MessageCounters> LoadCountersAction
        {
            get { return LoadCounters; }
        }

        public Func<bool, MessageCounters, IMsg> LoadTopicsAsChildrenFunc
        {
            get { return LoadTopicsAsChildren; }
        }

        public IServiceProvider ServiceProvider
        {
            get { return provider; }
        }

        public INavigationTreeItemInfo GetItemInfo(string itemId)
        {
            return forumInfo;
        }

        public Control CreateGuiControl()
        {
           return null;
        }


        //public IDisposable SubscribeNodesChanged(IServiceProvider serviceProvider, IObserver<EventArgs> observer)
        //{
        //    //throw new NotImplementedException();
        //    return Disposable.Empty;
        //}

        #endregion

        /// <summary>
        /// Loads the counters only.
        /// </summary>
        /// <param name="id">The forum id.</param>
        internal void LoadCounters(int id, MessageCounters messageCounters)
        {
            NewMessages_Message root = new NewMessages_Message(ServiceProvider);
            using (new WaitCursor())
            {
                root.FillCounters();
            }
#if FRM_UPDATE_TRACE
            Trace.WriteLine("FRU: NewMessages.LoadCounters");         //MLHIDE
#endif
            messageCounters.MessagesCount = root.RepliesCount;
            messageCounters.Unread = root.UnreadCount;
            messageCounters.RepliesToMeUnread = root.UnreadCount;
        }

        /// <summary>
        /// Loads all topics as children in message.
        /// </summary>
        /// <param name="isLoadAll">if set to <c>true</c> [is load all].</param>
        /// <returns>root message</returns>
        protected IMsg LoadTopicsAsChildren(bool isLoadAll, MessageCounters messageCounters)
        {
            NewMessages_Message root = new NewMessages_Message(ServiceProvider);
            using (new WaitCursor())
            {
                root.FillChildrenWithMessages();
#if FRM_UPDATE_TRACE
                Trace.WriteLine(
                    string.Format(
                        "FRM:NewMessages:RepliesToMe Unread {0}, Unread {1}, summary {2}", //MLHIDE
                        root.RepliesToMeUnread,
                        root.Unread,
                        root.Count));
#endif
                messageCounters.MessagesCount = root.Count;
                messageCounters.Unread = root.Unread;
            messageCounters.RepliesToMeUnread = root.RepliesToMeUnread;
            }

            return root;
        }

        /// <summary>
        /// Determines whether [is update need] [the specified MSG in].
        /// </summary>
        /// <param name="msgIn">The MSG in.</param>
        /// <returns>
        ///   <c>true</c> if [is update need] [the specified MSG in]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsUpdateNeed(object msgIn)
        {
            if (msgIn is Msg)
            {
                Msg msg = msgIn as Msg;
            }

            // It was wish to check if msg included in current forum but forum implmented in an other way
            return true;
        }
    }
}

